/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import model.Sanpham;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author Tran Manh Linh
 */
public class SanphamDAO {

    public static ArrayList<Sanpham> getAllProducts() {
        ArrayList<Sanpham> listAllProducts = new ArrayList<Sanpham>();
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.openSession();
        try {
            Transaction trans = ss.getTransaction();
            String hql = "from Sanpham";
            trans.begin();
            Query query = ss.createQuery(hql);
            listAllProducts = (ArrayList<Sanpham>) query.list();
            ss.flush();
            trans.commit();
            return listAllProducts;
        } catch (Exception ex) {
            listAllProducts = null;
        } finally {
            ssFac.close();
            return listAllProducts;
        }
    }

    public static Sanpham getProduct(String masp) {
        Sanpham sp = new Sanpham();
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.openSession();
        Transaction trans = ss.getTransaction();
        try {
            String hql = "from Sanpham sp where sp.maSanPham =:masp";
            trans.begin();
            Query query = ss.createQuery(hql);
            query.setString("masp", masp);
            sp = (Sanpham) query.list().get(0);
            ss.flush();
            trans.commit();
            return sp;
        } catch (Exception e) {
            ssFac.close();
            return null;
        }
    }

    public static Boolean updateProduct(Sanpham NewProduct) {
        Boolean kq = false;
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.openSession();
        Transaction trans = ss.getTransaction();
        try {
            trans.begin();
            ss.saveOrUpdate(NewProduct);
            ss.flush();
            trans.commit();
        } catch (HibernateException ex) {
            System.out.println(ex.getMessage());
            kq = false;
        } finally {
            ssFac.close();
        }
        return kq;
    }

    public static ArrayList<Sanpham> quickSearch(String stringtimkiem) {
        ArrayList<Sanpham> listProductsFinded = new ArrayList<Sanpham>();
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.openSession();
        Transaction trans = ss.getTransaction();
        try{
            String hql = "from Sanpham sp where sp.tenSanPham like :str or sp.moTa like :str or sp.danhmucNhasanxuat.nhasanxuat.tenNhaSanXuat like :str";
            trans.begin();
            Query query = ss.createQuery(hql);
            query.setString("str", "%" + stringtimkiem + "%");
            listProductsFinded = (ArrayList<Sanpham>) query.list();
            ss.flush();
            trans.commit();
        }
        catch(HibernateException ex) {
            System.out.println(ex.getMessage());
        } finally {
            ssFac.close();
            return listProductsFinded;
        }
    }
    
    public static ArrayList<Sanpham> advancedSearch(String maDM, String maNSX, String recommend) {
       ArrayList<Sanpham> listProductsFinded = new ArrayList<Sanpham>();
       return listProductsFinded;
    }
    
    public static ArrayList<Sanpham> getClosingBidProduct() {
        ArrayList<Sanpham> listProductsFinded = new ArrayList<Sanpham>();
        SessionFactory ssFac = HibernateUtil.getSessionFactory();
        Session ss = ssFac.openSession();
        Transaction trans = ss.getTransaction();
        try{
            String hql = "from Sanpham sp order by sp.thoiDiemKetThuc";
            trans.begin();
            Query query = ss.createQuery(hql);
            query.setMaxResults(16);
            listProductsFinded = (ArrayList<Sanpham>) query.list();
            ss.flush();
            trans.commit();
        }
        catch(HibernateException ex) {
            System.out.println(ex.getMessage());
        } finally {
            ssFac.close();
            return listProductsFinded;
        }
    }
}
